package com.example.practice.activity;

import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;


import com.example.practice.R;
import com.example.practice.adapter.VideoListAdapterYl;
import com.example.practice.bean.VideoBeanYl;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class VideoListActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView yl_tv_intro, yl_tv_video,yl_tv_chapter_intro;
    private ListView yl_lv_video_list;
    private ScrollView yl_sv_chapter_intro;
    private VideoListAdapterYl yl_adapter;
    private List<VideoBeanYl> yl_videoList;
    private int yl_chapterId;
    private String yl_intro;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yl_video_list);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        yl_chapterId = getIntent().getIntExtra("id",0);
        yl_intro = getIntent().getStringExtra("intro");

        initData();
        init();
    }

    //    读取数据流,参数in是数据流
    private String read(InputStream in) {
        BufferedReader reader = null;
        StringBuilder sb =null;
        String line = null;
        try {
            sb = new StringBuilder();
            reader = new BufferedReader(new InputStreamReader(in));
            while ((line=reader.readLine()) !=null) {
                sb.append(line);
                sb.append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        } finally {
            try {
                if(in != null) {
                    in.close();
                }
                if(reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

    //    设置视频列表本地数据
    private void initData() {
        JSONArray jsonArray;
        InputStream is =null;
        try{
            is = getResources().getAssets().open("data.json");
            jsonArray =new JSONArray(read(is));
            yl_videoList = new ArrayList<VideoBeanYl>();
            Log.i("yl_chapterId", yl_chapterId+"");
            for (int i=0; i<jsonArray.length();i++) {
                VideoBeanYl bean = new VideoBeanYl();
                JSONObject jsonObj = jsonArray.getJSONObject(i);
//                Log.i("jsonObj", jsonObj.getInt("chapterId")+200+"");
//                Log.i("yl_chapterId", yl_chapterId+"");
                if (jsonObj.getInt("chapterId")==yl_chapterId) {
                    bean.chapterId=jsonObj.getInt("chapterId");
                    bean.videoId=Integer.parseInt(jsonObj.getString("videoId"));
                    bean.title=jsonObj.getString("title");
                    bean.secondTitle=jsonObj.getString("secondTitle");
                    bean.videoPath=jsonObj.getString("videoPath");
                    yl_videoList.add(bean);
                }
                bean=null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void init(){
        Log.i("yl_videoList",yl_videoList.toString());
        yl_tv_intro = findViewById(R.id.yl_tv_intro);
        yl_tv_video = findViewById(R.id.yl_tv_video);
        yl_lv_video_list = findViewById(R.id.yl_lv_video_list);
        yl_tv_chapter_intro = findViewById(R.id.yl_tv_chapter_intro);
        yl_sv_chapter_intro = findViewById(R.id.yl_sv_chapter_intro);
        yl_adapter = new VideoListAdapterYl(this);
        yl_lv_video_list.setAdapter(yl_adapter);
        yl_tv_intro.setOnClickListener(this);
        yl_tv_video.setOnClickListener(this);
        yl_adapter.setData(yl_videoList);
        yl_tv_chapter_intro.setText(yl_intro);
        yl_tv_intro.setBackgroundColor(Color.parseColor("#30b4ff"));
        yl_tv_video.setBackgroundColor(Color.parseColor("#ffffff"));
        yl_tv_intro.setTextColor(Color.parseColor("#ffffff"));
        yl_tv_video.setTextColor(Color.parseColor("#000000"));
    }

    //控件的点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.yl_tv_intro: //简介
                yl_lv_video_list.setVisibility(View.GONE);
                yl_sv_chapter_intro.setVisibility(View.VISIBLE);
                yl_tv_intro.setBackgroundColor(Color.parseColor("#30b4ff"));
                yl_tv_video.setBackgroundColor(Color.parseColor("#ffffff"));
                yl_tv_intro.setTextColor(Color.parseColor("#ffffff"));
                yl_tv_video.setTextColor(Color.parseColor("#000000"));
                break;
            case R.id.yl_tv_video:  //视频
                yl_lv_video_list.setVisibility(View.VISIBLE);
                yl_sv_chapter_intro.setVisibility(View.GONE);
                yl_tv_intro.setBackgroundColor(Color.parseColor("#ffffff"));
                yl_tv_video.setBackgroundColor(Color.parseColor("#30b4ff"));
                yl_tv_intro.setTextColor(Color.parseColor("#000000"));
                yl_tv_video.setTextColor(Color.parseColor("#ffffff"));
                break;
            default:
                break;
        }
    }
}